using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using TaugeLibrary.Interfaces;
using TaugeFramework.Classes;
using TaugeFramework.BusinessUnit.User;
using TaugeFramework.BusinessUnit.Test;

namespace TaugeFramework
{
    public partial class frmMain : Form
    {
        ToolStripMenuItem[] aMenu;

        public frmMain()
        {
            InitializeComponent();
        }

        private void miApplicationOpen_Click(object sender, EventArgs e)
        {
            miCloseApplication_Click(null, null);

            mnuMain.Items.Remove(miWindow);

            Assembly assembly = Assembly.LoadFrom("TaugeStock.dll");

            Type type = assembly.GetType("TaugeStock.App");

            ITaugeApplication app = (ITaugeApplication)Activator.CreateInstance(type);
            app.Load();
            app.setFramework(Program.Framework);
            aMenu = app.getMenu();

            foreach (ToolStripMenuItem x in aMenu)
            {
                mnuMain.Items.Add(x);
            }
            mnuMain.Items.Add(miWindow);

        }

        private void miCloseApplication_Click(object sender, EventArgs e)
        {
            if (aMenu !=null)
                foreach (ToolStripMenuItem x in aMenu)
                    mnuMain.Items.Remove(x);
            
        }

        private void miTest_Click(object sender, EventArgs e)
        {
            Program.Framework.ShowBusinessUnitForm(new TestBUManager());
            Program.Framework.ShowBusinessUnitListForm(new TestBUManager());
            /*
            User x = new User();
            x.UserName = "rudin harianto";
            x.Password = "abcde";
            x.Description = "ini adalah user test";
            x.UserType = eUserType.USER;
            x.IsActive = true;
            Form f = UIManager.createUI(x);
            UIManager.writeUI(x, f);
            f.MdiParent = this;
            f.Show();
            */
            

            //ORM x = new ORM();
            //x.CreateTable(new User());
        }

        private void miUser_Click(object sender, EventArgs e)
        {
            Program.Framework.ShowBusinessUnitForm( new UserAccountManager());
            
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.mnuMain.MdiWindowListItem = miWindow;
            /*
            Program.AppPath = Application.StartupPath;
            Program.LoadINI(Application.StartupPath + @"\default.ini");
             * */

        }

        public void RefreshToolbar()
        {
            if (this.ActiveMdiChild is IRefreshAble)
                this.tbRefresh.Enabled = ((IRefreshAble)this.ActiveMdiChild).isRefreshable();
            else
                this.tbRefresh.Enabled = false;

            if (this.ActiveMdiChild is ISearchable)
                this.tbSearch.Enabled = ((ISearchable)this.ActiveMdiChild).isSearchable();
            else
                this.tbSearch.Enabled = false;



            if (this.ActiveMdiChild is IActionable)
            {
                this.tbOpen.Enabled = ((IActionable)this.ActiveMdiChild).isQueryable();
                this.tbInsert.Enabled = ((IActionable)this.ActiveMdiChild).isInsertable();
                this.tbUpdate.Enabled = ((IActionable)this.ActiveMdiChild).isUpdateable();
                this.tbDelete.Enabled = ((IActionable)this.ActiveMdiChild).isDeleteable();

            }
            else
            {
                this.tbOpen.Enabled = false;
                this.tbInsert.Enabled = false;
                this.tbUpdate.Enabled = false;
                this.tbDelete.Enabled = false;

            }

            this.tbCancel.Enabled = true;

            if (this.ActiveMdiChild is ISaveAble)
                this.tbSave.Enabled = ((ISaveAble)this.ActiveMdiChild).isSaveable();
            else
                this.tbSave.Enabled = false;

            if (this.ActiveMdiChild is IPostable)
            {
                this.tbPosted.Enabled = ((IPostable)this.ActiveMdiChild).isPostable();
                this.tbUnPost.Enabled = ((IPostable)this.ActiveMdiChild).isUnPostable();

            }
            else
            {
                this.tbPosted.Enabled = false;
                this.tbUnPost.Enabled = false;

            }

            if (this.ActiveMdiChild is IPrintable)
            {
                this.tbPrint.Enabled = ((IPrintable)this.ActiveMdiChild).isPrintable();
            }
            else
            {
                this.tbPrint.Enabled = false;
            }


            if (this.ActiveMdiChild is INavigateable)
            {
                this.tbNext.Enabled = ((INavigateable)this.ActiveMdiChild).isNavigateable();
                this.tbPrev.Enabled = this.tbNext.Enabled;
                this.tbFirst.Enabled = this.tbNext.Enabled;
                this.tbLast.Enabled = this.tbNext.Enabled;
            }
            else
            {
                this.tbNext.Enabled = false;
                this.tbPrev.Enabled = false;
                this.tbFirst.Enabled = false;
                this.tbLast.Enabled = false;
            }
        }

        private void frmMain_MdiChildActivate(object sender, EventArgs e)
        {
            RefreshToolbar();            
        }

        private void tbRefresh_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is IRefreshAble)
            {
                ((IRefreshAble)this.ActiveMdiChild).doRefresh();
            }
        }

        private void tbSearch_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is ISearchable)
            {
                ((ISearchable)this.ActiveMdiChild).doSearch();
            }
        }

        private void tbOpen_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is IActionable )
            {
                ((IActionable)this.ActiveMdiChild).doQuery();
            }
        }

        private void tbInsert_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is IActionable)
            {
                ((IActionable)this.ActiveMdiChild).doInsert();
            }
        }

        private void tbUpdate_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is IActionable)
            {
                ((IActionable)this.ActiveMdiChild).doUpdate();
            }
        }

        private void tbDelete_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is IActionable)
            {
                ((IActionable)this.ActiveMdiChild).doDelete();
            }
        }

        private void tbUnPost_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is IPostable)
            {
                ((IPostable)this.ActiveMdiChild).doPosting();
            }
        }

        private void tbPosted_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is IPostable)
            {
                ((IPostable)this.ActiveMdiChild).doUnPosting();
            }

        }

        private void tbPrint_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is IPrintable)
            {
                ((IPrintable)this.ActiveMdiChild).doPrint();
            }

        }

        private void tbFirst_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is INavigateable)
                ((INavigateable)this.ActiveMdiChild).doMoveFirst();
        }

        private void tbPrev_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is INavigateable)
                ((INavigateable)this.ActiveMdiChild).doMovePrev();
        }

        private void tbNext_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is INavigateable)
                ((INavigateable)this.ActiveMdiChild).doMoveNext();
        }

        private void tbLast_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is INavigateable)
                ((INavigateable)this.ActiveMdiChild).doMoveLast();
        }

        private void tbCancel_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                if (this.ActiveMdiChild is ICancelable)
                    ((ICancelable)this.ActiveMdiChild).doCancel();
                else
                    this.ActiveMdiChild.Close();
            }
        }

        private void tbSave_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is ISaveAble)
                ((ISaveAble)this.ActiveMdiChild).doSave();
        }
    }
}